package com.travel.order.web;

import com.travel.common.utils.R;
import com.travel.order.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class OrderWebController {

    @Autowired
    TicketService ticketService;

    /**
     * 立即预定
     * 判断是否有足够库存
     * @param spuId
     * @param selectedAttrs
     * @param redirectAttributes
     * @return
     */
    @PostMapping("/buyTicket")
    @ResponseBody
    public R buyTicket(@RequestParam("spuId")Integer spuId,
                       @RequestParam("selectedAttrs") String selectedAttrs,
                       RedirectAttributes redirectAttributes){

        Boolean b = ticketService.buyTicket(spuId,selectedAttrs);
        if(b){ // 成功
            System.out.println("有库存");
            return R.ok().put("hasStock",true);
        }else{ // 失败
            System.out.println("无库存");
            return R.ok().put("hasStock",false);
        }
    }
}
